package com.ruoyi.statistics.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.course.controller.LabCourseController;
import com.ruoyi.statistics.domain.LabStatisticsEquipment;
import org.apache.poi.util.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.statistics.domain.LabStatisticsProject;
import com.ruoyi.statistics.service.ILabStatisticsProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 教学试验项目Controller
 * 
 * @author jun.wang
 * @date 2022-05-21
 */
@RestController
@RequestMapping("/statistics/project")
public class LabStatisticsProjectController extends BaseController
{
    @Autowired
    private ILabStatisticsProjectService labStatisticsProjectService;

    /**
     * 查询教学试验项目列表
     */
//    @PreAuthorize("@ss.hasPermi('statistics:statisticsProject:list')")
    @GetMapping("/list")
    public TableDataInfo list(LabStatisticsProject labStatisticsProject)
    {
        startPage();
        List<LabStatisticsProject> list = labStatisticsProjectService.selectLabStatisticsProjectList(labStatisticsProject);
        return getDataTable(list);
    }

    /**
     * 导出教学试验项目列表
     */
//    @PreAuthorize("@ss.hasPermi('statistics:statisticsProject:export')")
    @Log(title = "教学试验项目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabStatisticsProject labStatisticsProject)
    {
        List<LabStatisticsProject> list = labStatisticsProjectService.selectLabStatisticsProjectList(labStatisticsProject);
        ExcelUtil<LabStatisticsProject> util = new ExcelUtil<LabStatisticsProject>(LabStatisticsProject.class);
        return util.exportExcel(list, "statisticsProject");
    }

    /**
     * 获取教学试验项目详细信息
     */
//    @PreAuthorize("@ss.hasPermi('statistics:statisticsProject:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(labStatisticsProjectService.selectLabStatisticsProjectById(id));
    }

    /**
     * 新增教学试验项目
     */
//    @PreAuthorize("@ss.hasPermi('statistics:statisticsProject:add')")
    @Log(title = "教学试验项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabStatisticsProject labStatisticsProject)
    {
        return toAjax(labStatisticsProjectService.insertLabStatisticsProject(labStatisticsProject));
    }

    /**
     * 修改教学试验项目
     */
//    @PreAuthorize("@ss.hasPermi('statistics:statisticsProject:edit')")
    @Log(title = "教学试验项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabStatisticsProject labStatisticsProject)
    {
        return toAjax(labStatisticsProjectService.updateLabStatisticsProject(labStatisticsProject));
    }

    /**
     * 删除教学试验项目
     */
//    @PreAuthorize("@ss.hasPermi('statistics:statisticsProject:remove')")
    @Log(title = "教学试验项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(labStatisticsProjectService.deleteLabStatisticsProjectByIds(ids));
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Long baseCollegeId) throws Exception {
        ExcelUtil<LabStatisticsProject> util = new ExcelUtil<>(LabStatisticsProject.class);
        List<LabStatisticsProject> projectDtos = util.importExcel(file.getInputStream());
        labStatisticsProjectService.importData(projectDtos, baseCollegeId);
        return AjaxResult.success("导入成功");
    }

    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 下载名称
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "基表四学年教学实验项目汇总表（SJ4）.xls"));
        InputStream resourceAsStream = LabCourseController.class.getClassLoader()
                .getResourceAsStream("imp_template/基表四学年教学实验项目汇总表（SJ4）.xls");
        IOUtils.copy(resourceAsStream, response.getOutputStream());
    }
}
